home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / UUPC11QS.ARJ / NOVRSTRK.C < prev    next >
C/C++ Source or Header  |  1991-10-05  |  6KB  |  158 lines

  1. /*--------------------------------------------------------------------*/
  2. /*    Program:    noverstrk.c     14 August 1990                      */
  3. /*    Author:     Andrew H. Derbyshire                                */
  4. /*                108 Decatur St, Apt 9                               */
  5. /*                Arlington, MA 02174                                 */
  6. /*    Function:   Drop overstruck characters from a file              */
  7. /*    Arguments:  input file, output file.                            */
  8. /*--------------------------------------------------------------------*/
  9.  
  10. /*--------------------------------------------------------------------*/
  11. /*    Revised 10 March 1991 to handle overstriking via carriage       */
  12. /*    returns, which is how Word for Windows does it                  */
  13. /*--------------------------------------------------------------------*/
  14.  
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <stdio.h>
  18.  
  19. #include "lib.h"
  20. #include "timestmp.h"
  21.  
  22. /*--------------------------------------------------------------------*/
  23. /*    m a i n                                                         */
  24. /*                                                                    */
  25. /*    Main program                                                    */
  26. /*--------------------------------------------------------------------*/
  27.  
  28.  void main( int argc, char *argv[] )
  29.  {
  30.    char buf[BUFSIZ];       /* Our I/O buffer                         */
  31.    size_t column = 0;
  32.    size_t linesize = 0;
  33.    int argx = 1;           /* Current argument being processed       */
  34.    FILE *input;
  35.    FILE *output;
  36.    int done = 0;
  37.    int ignore = 0;
  38.  
  39. /*--------------------------------------------------------------------*/
  40. /*                        Announce our version                        */
  41. /*--------------------------------------------------------------------*/
  42.  
  43.    banner( argv );
  44.  
  45. /*--------------------------------------------------------------------*/
  46. /*                            Handle help                             */
  47. /*--------------------------------------------------------------------*/
  48.  
  49.    if (( argc > 1 ) && equal(argv[1],"-?"))
  50.    {
  51.       printf("Usage:\tnovrstrk\t infile outfile\n");
  52.       exit(1);
  53.    }
  54. /*--------------------------------------------------------------------*/
  55. /*                  Get the input file name, if any                   */
  56. /*--------------------------------------------------------------------*/
  57.  
  58.     if (argx == argc)
  59.       input = stdin;
  60.     else {
  61.       input = fopen(argv[argx++],"rb");
  62.       if (input == NULL)
  63.       {
  64.          perror(argv[--argx]);
  65.          exit (100);
  66.       }
  67.     }
  68.  
  69. /*--------------------------------------------------------------------*/
  70. /*                    Get output file name, if any                    */
  71. /*--------------------------------------------------------------------*/
  72.  
  73.     if (argx == argc )
  74.       output = stdout;
  75.     else {
  76.       output = fopen(argv[argx++],"w");
  77.       if (output == NULL)
  78.       {
  79.          perror(argv[--argx]);
  80.          exit( 200 );
  81.       } /* if */
  82.     } /* else */
  83.  
  84. /*--------------------------------------------------------------------*/
  85. /*       Main loop to drop our overstrikes                            */
  86. /*--------------------------------------------------------------------*/
  87.  
  88.     while (!done)
  89.     {
  90.       char c = fgetc( input );
  91.       if ( feof(input) || ferror(input) )
  92.          done = 1;
  93.       else switch (c)
  94.       {
  95.          case '\b':           /* Simple overstrike?                  */
  96.             ignore = 1;       /* Yes --> Ignore it                   */
  97.             break;
  98.  
  99.          case '\r':           /* Carriage return?                    */
  100.             if ( column > linesize )
  101.                linesize = column;
  102.             column = 0;       /* Yes --> Start line over again       */
  103.             break;
  104.  
  105.          case '\f':           /* Form feed?                          */
  106.          case '\n':           /* New line?                           */
  107.             if ( column > linesize )
  108.                linesize = column;
  109.             buf[linesize++] = c;  /* Yes --> Add the end of line     */
  110.             buf[linesize] = '\0'; /*   ... terminate the buffer      */
  111.             fputs(buf, output);   /*   ... write it out              */
  112.             done = ferror(output);/*   ... check the result          */
  113.             linesize = column = 0;/*   ... and begin a new buffer    */
  114.             break;
  115.  
  116.          default:
  117.             if ( ignore )
  118.                ignore = 0;
  119.             else if (( linesize > column ) && (c == ' '))
  120.                column += 1;
  121.             else
  122.                buf[ column++ ] = c; /* Add the character to the buf */
  123.        break;
  124.        } /* switch */
  125.     } /* while */
  126.  
  127. /*--------------------------------------------------------------------*/
  128. /*                   Check for errors on the files                    */
  129. /*--------------------------------------------------------------------*/
  130.  
  131.    if (ferror(input))
  132.    {
  133.       perror(argv[argc > 1 ? 1 : 0]);
  134.       clearerr(input);
  135.    }
  136.  
  137.    if ((column > 0) && !ferror(output))
  138.    {
  139.       fputs(buf, output );
  140.       buf[linesize] = '\0';
  141.    }
  142.  
  143.    if (ferror(output))
  144.    {
  145.       perror(argv[argc > 2 ? 2 : 0]);
  146.       clearerr(output);
  147.    }
  148.  
  149. /*--------------------------------------------------------------------*/
  150. /*                       Close up shop and exit                       */
  151. /*--------------------------------------------------------------------*/
  152.  
  153.    fclose(input);
  154.    fclose(output);
  155.  
  156.    exit (0);
  157.  } /* main */
  158.